package movie.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import movie.entity.Film;
import movie.entity.OrderDetail;
import movie.service.FilmFindAllService;
import movie.service.OrderDetailService;

@Controller
public class FilmFindAllController {

	@Autowired
	private FilmFindAllService filmFindAllService;
	@Autowired
	private OrderDetailService orderDetailService;
	
	@RequestMapping(value = "/filmFindAll", method = RequestMethod.GET)
	public String get(Model model) {
		List<Film> films = filmFindAllService.findAll();
		for(Film film:films)
		{
			
			SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");		
			film.setReleaseDateString(format1.format(film.getReleaseDate()));	

		}

		model.addAttribute("films", films);
		model.addAttribute("OrderNumber",orderDetailService.findOrderNumber());
		List<OrderDetail> orderDetails=orderDetailService.findPrice();
		for(OrderDetail orderDetail11:orderDetails)
		{
			SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");		
			orderDetail11.setOrderDateString(format1.format(orderDetail11.getOrderDate()));			
		}
		model.addAttribute("price",orderDetails);
		return "Film/Filmlist";
	}
	
	@RequestMapping(value = "/filmFindAll", method = RequestMethod.POST)
	public String post(Model model,String filmName) {

		model.addAttribute("OrderNumber",orderDetailService.findOrderNumber());
		List<OrderDetail> orderDetails=orderDetailService.findPrice();
		for(OrderDetail orderDetail11:orderDetails)
		{
			SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");		
			orderDetail11.setOrderDateString(format1.format(orderDetail11.getOrderDate()));			
		}
		model.addAttribute("price",orderDetails);
		
		if(!filmName.equals(""))
		{
			List<Film> films = filmFindAllService.findByFilmName(filmName);
			for(Film film:films)
			{
				
				SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");		
				film.setReleaseDateString(format1.format(film.getReleaseDate()));	

			}
			model.addAttribute("films", films);
			return "Film/Filmlist";
		}
		else 
		{
			List<Film> films = filmFindAllService.findAll();
			for(Film film:films)
			{
				
				SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");		
				film.setReleaseDateString(format1.format(film.getReleaseDate()));	

			}
			model.addAttribute("films", films);
			return "Film/Filmlist";
		}
	}
	
	
	@RequestMapping(value = "/filmupdate", method = RequestMethod.GET)
	public String getUpdate(Model model,int movieId) {
		Film film = filmFindAllService.findMovieId(movieId);
		SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");		
		film.setReleaseDateString(format1.format(film.getReleaseDate()));
		model.addAttribute("film", film);
		
		return "Film/Filmupdate";
	}
	
	@RequestMapping(value = "/filmupdate", method = RequestMethod.POST)
	public String postUpdate(Film film,Model model) {
	
		if(film.getFilmName().equals("")|| film.getDirector().equals("") || film.getCover().equals("") || film.getType().equals("") 
				|| film.getLeads().equals("")|| film.getCountry().equals("")|| film.getFilmLength()==null || 
				film.getReleaseDate()==null || film.getScore()== 0 || film.getIntroduction().equals("") ||
				film.getReying()==null)
		{
			Film film11 = filmFindAllService.findMovieId(film.getMovieId());
			SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
			film11.setReleaseDateString(format1.format(film11.getReleaseDate()));
			model.addAttribute("film", film11);
			model.addAttribute("cuowu", 1);
			return "Film/Filmupdate";
		}
		int rows = filmFindAllService.update(film);
		return "redirect:/filmFindAll";
	}
	/**
	 * 返回热映电影信息
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/filmBeShowing", method = RequestMethod.GET)
	public String getShowingfilms(Model model) {
		
		List<Film> filmsReYing=filmFindAllService.findReYing();
		List<Film> filmsWillShowing=filmFindAllService.findWillShow();
		model.addAttribute("filmsReYing",filmsReYing);
		model.addAttribute("filmsWillShowing",filmsWillShowing);
		return "Film/filmBeShowing";
	}
	/**
	 * 返回即将上映电影信息
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/filmWillShow", method = RequestMethod.GET)
	public String getWillShowfilms(Model model) {
		List<Film> filmsWillShowing=filmFindAllService.findWillShow();
		model.addAttribute("filmsWillShowing",filmsWillShowing);
		return "Film/filmWillShow";
	}
	/**
	 * 返回经典电影信息
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/filmShowed", method = RequestMethod.GET)
	public String getShowedfilms(Model model) {
		List<Film> filmsShowed=filmFindAllService.findShowed();
		model.addAttribute("filmsShowed",filmsShowed);
		return "Film/filmShowed";
	}
	
	
	
}
